import msg from '@/utils/message';
import cache from '@/utils/cache';
import config from '@/config';
module.exports = {
	...config.http,
	
	get(url, data = {}, heads = {}) {
		let token = cache.getToken();
		token && (
			data[this.TOKEN_KEY_NAME] = token
		);
		let app=this
		return new Promise((resolve, reject) => {
			uni.showLoading({
				mask: true,
				title: '加载中'
			});
			uni.request({
				url: this.SERVER + url,
				method: 'GET',
				data: data,
				header: {
					...heads
				},
				success: res => {
				    if (showLoading) { // 判断是否隐藏loading动画
				        uni.hideLoading();
				    }
				    const responseData = res.data;
				    if (responseData[this.CODE_KEY_NAME] == this.LOGIN_OUT_CODE) {
				        cache.setToken('');
					
				        uni.showModal({
				            title: '提示',
				            content: '此处内容需要您登录',
				            success: function(res) {
				                if (res.confirm) {
				                    uni.redirectTo({
				                        url: app.LOGIN_OUT_VIEW
				                    });
				                } else if (res.cancel) {
				                    console.log('用户点击取消');
				                    uni.navigateBack();
				                }
				            }
				        });
					
				        return;
				    }
					
				    if (responseData[this.CODE_KEY_NAME] != this.OK_CODE) {
				        msg.msgError(responseData.msg || '出错了');
				        reject(responseData.msg || '出错了');
				        return;
				    }
				    resolve(responseData);
				},
				fail: env => {
					uni.hideLoading()
					msg.msgError('出错了');
					reject('出错了');
				}
			});
		});

	},
	post(url, data = {},showLoading=true, formData, heads = {}) {
	    let token = cache.getToken();
	    token && (
	        data[this.TOKEN_KEY_NAME] = token
	    );
	    let app = this;
	    return new Promise((resolve, reject) => {
	        if (showLoading) { // 判断是否显示loading动画
	            uni.showLoading({
	                mask: true,
	                title: '加载中'
	            });
	        }
	        uni.request({
	            url: this.SERVER + url,
	            method: 'POST',
	            data: data,
	            header: {
	                ...heads,
	                'content-type': formData ? 'application/x-www-form-urlencoded' : this.CONTENT_TYPE,
	                'token': token
	            },
	            success: res => {
	                if (showLoading) { // 判断是否隐藏loading动画
	                    uni.hideLoading();
	                }
	                const responseData = res.data;
	                if (responseData[this.CODE_KEY_NAME] == this.LOGIN_OUT_CODE) {
	                    cache.setToken('');
	
	                    uni.showModal({
	                        title: '提示',
	                        content: '此处内容需要您登录',
	                        success: function(res) {
	                            if (res.confirm) {
	                                uni.redirectTo({
	                                    url: app.LOGIN_OUT_VIEW
	                                });
	                            } else if (res.cancel) {
	                                console.log('用户点击取消');
	                                uni.navigateBack();
	                            }
	                        }
	                    });
	
	                    return;
	                }
	
	                if (responseData[this.CODE_KEY_NAME] != this.OK_CODE) {
	                    msg.msg(responseData.msg );
	                    reject(responseData.msg);
	                    return;
	                }
	                resolve(responseData);
	            },
	            fail: env => {
	                if (showLoading) { // 判断是否隐藏loading动画
	                    uni.hideLoading();
	                }
	                msg.msgError('出错了');
	                reject('出错了');
	            }
	        });
	    });
	},

	uploadFile(filePath) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: this.SERVER + this.UPLOAD_URL,
				name: 'file',
				filePath: filePath,
				success: res => {
					const data = JSON.parse(res.data);
					console.log('res.data',data);
					// if (data[this.CODE_KEY_NAME] != this.LOGIN_OUT_CODE) {
					// 	cache.setToken('');

					// 	uni.redirectTo({
					// 		url: this.LOGIN_OUT_VIEW
					// 	});

					// 	return;
					// }

					// if (data[this.CODE_KEY_NAME] == this.OK_CODE) {
					// 	msg.msgError(data.msg || '上传失败');
					// 	reject(data.msg || '上传失败');
					// 	return;
					// }
					resolve(data.data);
				},
				fail: env => {
					msg.msgError('上传失败');
					reject('上传失败');
				}
			});
		});
	}
};